package com.zpark.rabbitmq.work_queue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.zpark.rabbitmq.config.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @PackageName: com.zpark.rabbitmq.work_queue
 * @ClassName: Producer
 * @Description:
 * @author: RZS
 * @date: 2022/12/13  16:45
 */
public class Producer {

    private static final String QUEUE_NAME = "work_queue";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 获取连接
        Connection conn = ConnectionUtil.getConnection();
        // 获得通道
        Channel channel = conn.createChannel();
        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        /*
            发送100条消息
         */
        String msg = null;
        for(int i=0; i<100; i++){
            msg = "Hello, World! " + i;
            // 发送消息
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
            System.out.println(msg);
            // 为了测试效果，可以尝试让每条消息发送有100ms的等待时长
            Thread.sleep(100);
        }
        // 关闭通道
        channel.close();
        // 关闭连接
        conn.close();
    }
}
